package com.zadtek.shopmall.activities;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.goyourfly.multi_picture.ImageLoader;
import com.goyourfly.multi_picture.MultiPictureView;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelector;

public class ChoosePicActivity extends AbsActivity {
    private ImageView icBack;
    private TextView topTitle,tvConfirm;
    private MultiPictureView multiPictureView;
    private ArrayList<String> choosePath = new ArrayList<>();
    public final static String IMGS = "imgs";
    @Override
    public int setView() {
        return R.layout.activity_choose_pic;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        ArrayList<String> list = getIntent().getStringArrayListExtra(IMGS);
        if(list!=null && list.size()!=0){
            choosePath.addAll(list);
        }
        initView();
        initLogic();
        updateView();
    }

    private void updateView() {

        multiPictureView.clearItem();
        for(String path:choosePath){
            File file = new File(path);
            Uri uri = Uri.fromFile(file);
            multiPictureView.addItem(uri);
        }
    }


    private void initLogic() {
        MultiPictureView.setImageLoader(new ImageLoader() {
            @Override
            public void loadImage(@NotNull ImageView image, @NotNull Uri uri) {
                Glide.with(ChoosePicActivity.this)
                        .load(uri)
                        .into(image);
            }
        });
        multiPictureView.setAddClickCallback(new MultiPictureView.AddClickCallback() {
            @Override
            public void onAddClick(@NotNull View view) {
                performCodeWithPermission("需要相机权限", new PermissionCallback() {
                    @Override
                    public void hasPermission() {
                        MultiImageSelector.create().showCamera(true).multi().count(9-choosePath.size()).start(ChoosePicActivity.this,200);
                    }

                    @Override
                    public void noPermission() {

                    }
                }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA});
            }
        });
        multiPictureView.setDeleteClickCallback(new MultiPictureView.DeleteClickCallback() {
            @Override
            public void onDeleted(@NotNull View view, int index) {
                choosePath.remove(index);
                multiPictureView.removeItem(index);
            }
        });
    }

    private void initView() {
        icBack = findViewById(R.id.ic_back);
        topTitle = findViewById(R.id.title);
        multiPictureView = findViewById(R.id.multi_view);
        topTitle.setText("选择图片");
        icBack.setVisibility(View.VISIBLE);
        icBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        tvConfirm = findViewById(R.id.tv_confirm);
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putStringArrayListExtra(IMGS,choosePath);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == 200){
            ArrayList<String> list = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
            choosePath.addAll(list);
            updateView();
        }
    }
}
